import sys
import os

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from project_utils.mysql_conncet import ReadFromMysql
from config import *

mysql = ReadFromMysql(host=mysqlhost, user=user, password=password, port=3306)
mysql.makepool(database)


def mappings(history=True, mqtt=False):
    conn = mysql.getconn()
    sc = conn.cursor()
    dy_code = 'ac_mqtt' if mqtt else 'ac'
    column = ["original_field", "natrual_field", "data_type"]
    if history:
        sql = f""" SELECT original_field,natrual_field, data_type
                FROM {mongo_map_table}
                where dy_code='{dy_code}'  """
    else:
        sql = f""" SELECT original_field,natrual_field, data_type
                        FROM {hbase_map_table}
                        where dy_code='{dy_code}'  """
    sc.execute(sql)
    conn.commit()
    result = sc.fetchall()
    result = [dict(zip(column, i)) for i in result]
    return result
